package com.starsoft.smdc.web;

import java.io.IOException;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.starsoft.frame.core.orm.PageObject;
import com.starsoft.frame.core.web.WebUtil;
import com.starsoft.frame.util.DateUtil;
import com.starsoft.frame.util.StringUtil;
import com.starsoft.smdc.bean.SmdcDiv;
import com.starsoft.smdc.bean.SmdcDivId;
import com.starsoft.smdc.bean.SmdcSecurity;
import com.starsoft.smdc.crawler.SmdcUpdateService;
import com.starsoft.smdc.service.DivService;
import com.starsoft.smdc.service.MarketDailyService;
import com.starsoft.smdc.service.SecurityService;

@RestController
@RequestMapping(value = "/smdc/div")
public class DivController {

	@Autowired
	DivService divService;
	
	@Autowired
	SmdcUpdateService updateService;
	
	@Autowired
	SecurityService securityService;
	
	@Autowired
	MarketDailyService marketDailyService;
	
	@RequestMapping(value="/page")
	public Map<String, Object> pageQuery(HttpServletRequest request){
		PageObject po = WebUtil.getPageObject(request);
		String secId = request.getParameter("secId");
		Date beginDate = WebUtil.getDate(request, "beginDate", "yyyy-MM-dd");
		Date endDate = WebUtil.getDate(request, "endDate", "yyyy-MM-dd");
		return divService.pageQuery(po, secId, beginDate, endDate);
	}
	
	@RequestMapping(value="/update")
	public Map<String, Object> update(HttpServletRequest request){
		int num=0;
		String secId = request.getParameter("secId");
		if(StringUtil.isEmpty(secId)){
			num = updateService.updateDivAll().size();
		}else{
			SmdcSecurity security = securityService.get(secId);
			Date beginDate = null;
			String beginDateStr = request.getParameter("beginDate");
			if(!StringUtil.isEmpty(beginDateStr)){
				beginDate = DateUtil.toDate(beginDateStr, "yyyy-MM-dd");
			} else{
				beginDate = divService.getBeginDate(security);
			}
			num = updateService.updateDiv(security, beginDate).size();
		}
		return WebUtil.sucesseModel("更新成功:"+num);
	}
	
	@RequestMapping(value="/datayesupload")
	public Map<String, Object> datayesUpload(@RequestParam(value = "file") MultipartFile file) throws IOException{
		return WebUtil.errorModel("Not support");
	}
	
	@RequestMapping(value="/delete")
	public Map<String, Object> delete(HttpServletRequest request, SmdcDiv div){
		String secId = request.getParameter("secId");
		Date divDate = WebUtil.getDate(request, "divDate", "yyyy-MM-dd");
		SmdcDivId divId = new SmdcDivId(secId, divDate);
		div.setId(divId);
		divService.delete(div);
		return WebUtil.sucesseModel(null);
	}
	
	@RequestMapping(value="/save")
	public Map<String, Object> save(HttpServletRequest request, SmdcDiv div){
		Date divDate = WebUtil.getDate(request, "divDate", "yyyy-MM-dd");
		String secId = request.getParameter("secId");
		SmdcDivId id = new SmdcDivId(secId, divDate);
		div.setId(id);
		divService.save(div);
		return WebUtil.sucesseModel("保存成功");
	}
	
}
